import API from '@/utils/backend_api'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    userId: '',
    avatar: '',
    username: '',
    roles: [],
    workplace: '',
    department: '',
    ranks: [],
    usersList: []
  },

  mutations: {
    SET_WORKPLACE: (state, workplace) => {
      state.workplace = workplace
    },
    SET_DEPARTMENT: (state, department) => {
      state.department = department
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER_ID: (state, user_id) => {
      state.userId = user_id
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_RANKS: (state, ranks) => {
      state.ranks = ranks
    },
    SET_ALL_USERS: (state, usersList) => {
      state.usersList = usersList
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        API.login.auth(username, userInfo.password).then(response => {
          console.log('login:' + response.data)
          setToken(response.data)
          commit('SET_TOKEN', response.data)
          resolve()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        API.sys.get_login_user().then(response => {
          console.log('getinfo:')
          const data = response.data
          console.log(data)
          if (data['_roles'] && data['_roles'].length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data['_roles'])
          } else {
            reject('登入账号没有访问页面的权限，请找系统管理员！')
          }
          if (data['_ranks'] && data['_ranks'].length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_RANKS', data['_ranks'])
          } else {
            reject('登入账号没有设置岗位权限，请找系统管理员！')
          }
          commit('SET_WORKPLACE', data.office)
          commit('SET_DEPARTMENT', data.department_name)
          commit('SET_USER_ID', data.id)
          commit('SET_AVATAR', data.user_avatar_url)
          commit('SET_USERNAME', data.username)
          commit('SET_ALL_USERS', data['_all_users'])
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        API.sys.logout().then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_USERNAME', '')
          commit('SET_ALL_USERS', [])
          commit('SET_RANKS', [])
          commit('SET_WORKPLACE', '')
          commit('SET_DEPARTMENT', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
